package com.learning.optimize.jdk.regex;

import java.util.*;
import java.util.regex.*;

public class CareerCodeMatcher {
    private Pattern basicPattern;
    private Pattern precisePattern;
    private Pattern groupPattern;

    public CareerCodeMatcher() {
        // 基础模式：匹配 [任意单词] + 固定后缀
        this.basicPattern = Pattern.compile("\\[(\\w+)\\](?:的职业代码|是几类职业|职业代码是多少)");

        // 精确模式：只匹配指定的前缀
        this.precisePattern = Pattern.compile("\\[(zhiyecc1|zhiyecc2|code)\\](?:的职业代码|是几类职业|职业代码是多少)");

        // 分组匹配模式：分别提取前缀和问题类型
        this.groupPattern = Pattern.compile("\\[(\\w+)\\](的职业代码|是几类职业|职业代码是多少)");
    }

    /**
     * 基础匹配 - 返回布尔值
     */
    public boolean matchBasic(String text) {
        return basicPattern.matcher(text).matches();
    }

    /**
     * 精确匹配 - 返回布尔值
     */
    public boolean matchPrecise(String text) {
        return precisePattern.matcher(text).matches();
    }

    /**
     * 分组提取 - 返回匹配的各个部分
     */
    public Map<String, String> extractGroups(String text) {
        Matcher matcher = groupPattern.matcher(text);
        if (matcher.matches()) {
            Map<String, String> result = new HashMap<>();
            result.put("full_match", matcher.group(0));
            result.put("code_prefix", matcher.group(1));  // zhiyecc1, zhiyecc2, code等
            result.put("question_type", matcher.group(2));  // 的问题类型部分
            return result;
        }
        return null;
    }

    /**
     * 批量匹配多个字符串
     */
    public List<Map<String, String>> findAllMatches(List<String> textList) {
        List<Map<String, String>> results = new ArrayList<>();
        for (String text : textList) {
            Map<String, String> matchInfo = extractGroups(text);
            if (matchInfo != null) {
                results.add(matchInfo);
            }
        }
        return results;
    }

    public static void main(String[] args) {
        CareerCodeMatcher matcher = new CareerCodeMatcher();

        // 测试数据
        List<String> testStrings = Arrays.asList(
                "[zhiyecc1]的职业代码",
                "[zhiyecc2]的职业代码",
                "[zhiyecc2]是几类职业",
                "[code]职业代码是多少",
                "[other]的职业代码",  // 这个在精确模式下不会匹配
                "无效字符串"
        );

        System.out.println("=== 基础匹配结果 ===");
        for (String testStr : testStrings) {
            System.out.println("'" + testStr + "': " + matcher.matchBasic(testStr));
        }

        System.out.println("\n=== 精确匹配结果 ===");
        for (String testStr : testStrings) {
            System.out.println("'" + testStr + "': " + matcher.matchPrecise(testStr));
        }

        System.out.println("\n=== 分组提取结果 ===");
        for (String testStr : testStrings) {
            Map<String, String> result = matcher.extractGroups(testStr);
            if (result != null) {
                System.out.println("'" + testStr + "': " + result);
            }
        }

        System.out.println("\n=== 批量匹配结果 ===");
        List<Map<String, String>> batchResults = matcher.findAllMatches(testStrings);
        for (Map<String, String> result : batchResults) {
            System.out.println(result);
        }
    }
}